package cn.lf.android.lawfirm.distribution.controller;


import android.content.Context;

import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.lf.android.lawfirm.AppConfig;
import cn.lf.android.lawfirm.BaseController;
import cn.lf.android.lawfirm.distribution.modle.TrialLevel;
import cn.lf.android.lawfirm.http.manager.EBCallback;
import cn.lf.android.lawfirm.http.manager.MyOkHttpClient;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.JsonBeans;
import cn.lf.android.lawfirm.utils.SPUtil;

/**
 * 列表
 */
public class TrialLevelConroller extends BaseController {
    TrialLevelCallback mTrialLevelCallback;
    private Context context;

    public TrialLevelConroller(TrialLevelCallback callback, Context context) {
        this.mTrialLevelCallback = callback;
        this.context = context;
    }


    /**
     * @param caseID
     * @param isDispatch
     */
    public void onTrialLevel(String caseID, String isDispatch) {
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("caseID", caseID);
        params.put("isDispatch", "true");
        DebugLog.e("caseID==" + caseID);
        new MyOkHttpClient().doGet(AppConfig.LsysCaseReviewLevel, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("审级列表==" + response);
                        JSONObject myJsonObject;
                        String arraylist;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                arraylist = myJsonObject.getJSONArray("responsObject")
                                        .toString();
                                List<TrialLevel> beans = JsonBeans
                                        .getJsonList(
                                                arraylist,
                                                new TypeToken<List<TrialLevel>>() {
                                                });
                                mTrialLevelCallback.onTrialLevelSuccessed(beans);
                            } else {
                                mTrialLevelCallback
                                        .onTrialLevelFailed();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        mTrialLevelCallback.onTrialLevelFailed();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }


    public interface TrialLevelCallback {
        public void onTrialLevelSuccessed(List<TrialLevel> beans);

        public void onTrialLevelFailed();

    }

}
